// import axios from 'axios'
var button = document.querySelector('button')
var originalText = button.innerHTML; // 保存原始按钮文本
//2.事件监听(绑定事件)
//事件源.addEventListener('事件',事件处理函数)
button.addEventListener('click', function() {
  // alert('点击了按钮事件')
  const phonedom = document.getElementById('phone')
  let phone = phonedom.value.trim()
  let pattern = /^(?:(?:\+|00)86)?1[3-9]\d{9}$/
  if (!pattern.test(phone)) {
     alert('请输入正确手机号码')
  } else {
      getcode(phone)
  }
})
// 设置定时器
function  setIntervalfun(params) {
  var seconds = 60; // 定时器时长，单位为秒
  button.disabled = true; // 禁用按钮
  var timer = setInterval(function() {
    button.innerHTML = seconds + ' 秒'; // 更新按钮文本为剩余秒数
    seconds--;
    if (seconds < 0) {
      clearInterval(timer); // 清除定时器
      button.innerHTML = originalText; // 恢复原始按钮文本
      button.disabled = false; // 启用按钮
    }
  }, 1000); // 每秒执行一次
}

//  获取验证码
function getcode(phone) {
  axios({
    method:'post',
    url: '/biz/pio-microservice-biz/wx/sendValidCode',
    data: {
      phone:phone
    },
  }).then(res => {
    if (res.data.code == 200) {
      setIntervalfun()
    }
    alert(`${res.data.message}`)
  })
}
// 确认绑定
const auredom = document.getElementById('submit')
auredom.addEventListener('click', function () {
  // 获取手机号码
  const phonedom = document.getElementById('phone')
  let phone = phonedom.value.trim()
  let pattern = /^(?:(?:\+|00)86)?1[3-9]\d{9}$/
  // 获取验证码
  const captchadom = document.getElementById('captcha')
  let code = captchadom.value.trim()
  // 判断手机号和验证码是否存在
  if (!pattern.test(phone)) {
    alert('请先输入正确手机号码')
  } else if (!code) {
    alert('请输入验证码')
  } else {
    wxbangding(phone,code)
  }
})
// 确认绑定接口
function wxbangding(phone, code) {
  // 获取类型
  var radio = document.getElementsByName("userType");
  let customerType = ''
  var openiddom = document.getElementById('openid')
  let openid=openiddom.value
	for (i=0; i<radio.length; i++) {
    if (radio[i].checked) {
      customerType=radio[i].value
		}
  }
  axios({
    url: '/biz/pio-microservice-biz/wx/bind',
    data: {
      phone: phone,
      validCode: code,
      customerType: customerType,
      openid
    },
    method:'post',
  }).then(res => {
    console.log(res);
    alert(`${res.data.message}`)
  })
}